// pages/num/num.js
Page({

  data: {
    list: [{
      id: 1,
      count: 0
    }, {
      id: 2,
      count: 0
    }, {
      id: 3,
      count: 0
    }, {
      id: 4,
      count: 0
    }],
    operator1: [{
      id: 1,
      op: '+'
    }, {
      id: 2,
      op: '+'
    }, {
      id: 3,
      op: '+'
    }],
    operator2: [{
      id: 1,
      op: '+'
    }, {
      id: 2,
      op: '+'
    }],
    operator3: '+',
    timer: '', //定时器名字
    countDownNum: '60', //倒计时初始值
    quiz: 0,
    quiz_t: 0,
    numlist1: [0, 0, 0],
    numlist2: [0, 0],
    num: 0,
    answer: '',
    MidNum:'',
  },

  onLoad: function(options) {
    this.setQuiz();
  },

  onShow: function() {
    this.countDown();
  },

  setQuiz: function() {
    let that = this;
    var i = 0;
    for (i = 0; i < 4; i++) {
      var num = Math.floor(Math.random() * 9) + 1;
      switch (i) {
        case 0:
          that.setData({
            'list[0].count': num
          });
          break;
        case 1:
          that.setData({
            'list[1].count': num
          });
          break;
        case 2:
          that.setData({
            'list[2].count': num
          });
          break;
        case 3:
          that.setData({
            'list[3].count': num
          });
          break;
      }
    };
    for (i = 0; i < 3; i++) {
      var num = Math.floor(Math.random() * 2) + 1;
      var op = '+';
      if (num != 1) {
        op = '+';
      } else {
        op = '-';
      };
      switch (i) {
        case 0:
          that.setData({
            'operator1[0].op': op
          });
          break;
        case 1:
          that.setData({
            'operator1[1].op': op
          });
          break;
        case 2:
          that.setData({
            'operator1[2].op': op
          });
          break;
      };
      var count = 0;
      if (op == '+') {
        count = that.data.list[i].count + that.data.list[i + 1].count;
      } else {
        count = that.data.list[i].count - that.data.list[i + 1].count;
      };
      switch (i) {
        case 0:
          that.setData({
            'numlist1[0]': count
          });
          break;
        case 1:
          that.setData({
            'numlist1[1]': count
          });
          break;
        case 2:
          that.setData({
            'numlist1[2]': count
          });
          break;
      }
    };
    for (i = 0; i < 2; i++) {
      var num = Math.floor(Math.random() * 2) + 1;
      var op = '+';
      if (num != 1) {
        op = '+';
      } else {
        op = '-';
      };
      switch (i) {
        case 0:
          that.setData({
            'operator2[0].op': op
          });
          break;
        case 1:
          that.setData({
            'operator2[1].op': op
          });
          break;
      };
      var count = 0;
      if (op == '+') {
        count = that.data.numlist1[i] + that.data.numlist1[i + 1];
      } else {
        count = that.data.numlist1[i] - that.data.numlist1[i + 1];
      };
      switch (i) {
        case 0:
          that.setData({
            'numlist2[0]': count
          });
          break;
        case 1:
          that.setData({
            'numlist2[1]': count
          });
          break;
      }
    };
    if ((Math.floor(Math.random() * 2) + 1) != 1) {
      var num = that.data.numlist2[0] + that.data.numlist2[1];
      that.setData({
        operator3: '+',
        num: num
      });
    } else {
      var num = that.data.numlist2[0] - that.data.numlist2[1];
      that.setData({
        operator3: '-',
        num: num
      });
    };

  },

  getNum: function(e) {
    this.setData({
      answer: e.detail.value
    })
  },

  next: function() {
    let that = this;
    var answer = that.data.answer;
    var num = that.data.num;
    var flag = false;
    if (answer == num) {
      let countDownNum = that.data.countDownNum;
      countDownNum += 15
      that.setData({
        quiz_t: that.data.quiz_t + 1,
        countDownNum: countDownNum
      })
      flag = true;
    }
    that.setData({
      answer: '',
      MidNum: '',
      quiz: that.data.quiz + 1
    })
    this.setQuiz();
    console.log(flag)
    return flag;
  },

  checkCount: function() {
    wx.setStorageSync('quiz', this.data.quiz);
    wx.setStorageSync('quiz_t', this.data.quiz_t);
    wx.redirectTo({
      url: '../result/result',
    })
  },

  countDown: function() {
    let that = this;
    //如果将定时器设置在外面，那么用户就看不到countDownNum的数值动态变化，所以要把定时器存进data里面
    that.setData({
      timer: setInterval(function() { //这里把setInterval赋值给变量名为timer的变量
        let countDownNum = that.data.countDownNum; //获取倒计时初始值
        //每隔一秒countDownNum就减一，实现同步
        countDownNum--;
        //然后把countDownNum存进data，好让用户知道时间在倒计着
        that.setData({
          countDownNum: countDownNum
        })
        //在倒计时还未到0时，这中间可以做其他的事情，按项目需求来
        if (countDownNum <= 0) {
          if (that.next()) {
            countDownNum == 15;
          } else {
            //这里特别要注意，计时器是始终一直在走的，如果你的时间为0，那么就要关掉定时器！不然相当耗性能
            //因为timer是存在data里面的，所以在关掉时，也要在data里取出后再关闭
            clearInterval(that.data.timer);
            //关闭定时器之后，可作其他处理codes go here
            that.checkCount();
          }
        }
      }, 1000)
    })
  }
})